const gulp = require("gulp");
const cssmin = require("gulp-cssmin");
const autoprefixer = require("gulp-autoprefixer");
const uglify = require("gulp-uglify");
const babel = require("gulp-babel");
const htmlmin = require("gulp-htmlmin");
const clean = require("gulp-clean");
const webserver = require("gulp-webserver");
const sass = require("gulp-sass-china");

//static的批量转存
function copyFileFn() {
    return gulp.src("./project/static/**/*")
        .pipe(gulp.dest("./server/static"))
}
exports.copyFile = copyFileFn;
//css压缩--前缀--css压缩
function cssFn() {
    return gulp.src("./project/css/**/*")
        .pipe(autoprefixer("last 2 version", "safari 5", "ie 8", "ie 9", "opera 12.1", "ios 6", "android 4"))
        .pipe(cssmin())
        .pipe(gulp.dest("./server/css"))
}
exports.css = cssFn;

// //定义sass
function sassFn() {
    return gulp.src("./project/sass/**/*")
        .pipe(sass())
        // .pipe(autoprefixer("last 2 version", "safari 5", "ie 8", "ie 9", "opera 12.1", "ios 6", "android 4"))
        // .pipe(cssmin())
        .pipe(gulp.dest("./server/css"))
}
exports.sass = sassFn;
// //js压缩--ES6转ES5--压缩js
function jsFn() {
    return gulp.src("./project/js/**/*")
        .pipe(babel({
            presets: ["@babel/env"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest("./server/js"))
}
exports.js = jsFn;
// //html压缩
function htmlFn() {
    return gulp.src("./project/pages/**/*")
        .pipe(htmlmin({
            removeEmptyAttributes: true,
            collapseWhitespace: true
        }))
        .pipe(gulp.dest("./server/pages"))
}
exports.html = htmlFn;
// //clean删除文件
function cleanFn() {
    return gulp.src("./server")
        .pipe(clean())
}
exports.clean = cleanFn;

function indexFn() {
    return gulp.src("./project/index.html")
        .pipe(gulp.dest("./server"))
}
exports.index = indexFn;
// //批量执行
exports.all = gulp.parallel(copyFileFn, cssFn, jsFn, htmlFn, indexFn, sassFn);
// //监听所有文件
function watchAll(qwe) {
    gulp.watch("./project/static/**/*", copyFileFn);
    gulp.watch("./project/css/**/*", cssFn);
    gulp.watch("./project/js/**/*", jsFn);
    gulp.watch("./project/pages/**/*", htmlFn);
    gulp.watch("./project/index.html", indexFn);
    gulp.watch("./project/sass/**/*", sassFn);
    qwe();
}
exports.watch = watchAll;